using System;
using System.Collections;
using Nullables;

namespace Sedna.Core.Controller.Configurations
{
	public class DataGridConfiguration : ClientScriptingConfiguration, IDataGridConfiguration
	{
		private IList columns;
		private bool showHeader = true;
		private bool showFooter = false;
		private int pageSize = 50;//changed to reflect the reality
		private IDataGridPagerStyleConfiguration pagerStyle = null;//null means pagins is not enabled
		private bool usePrefixKeyForPager = false;
		private bool allowCustomPaging = false;
		private bool allowPaging = true;
		private string operationParameterName = "imsgrid_operation";
		private string operationContextName = "imsdatagrid.operation-name";
		private IDataGridLineSelector lineSelector = FAKE_LINE_SELECTOR_INSTANCE;
		private IDataGridController controller;
      

	    
//		private string dataCollectionPath = null;

		private string dataGridStatePath=null;
		private string virtualItemCountBindPath=null;
		private string operationNameBindPath=null;
        private string operationNameUnbindPath = null;
		private string dataSourceBindPath=null;
		private string sortAscUnbindPath=null;
		private string sortExpressionUnbindPath=null;
		private string pageSizeUnbindPath=null;
		private string currentPageIndexUnbindPath=null;
		private string virtualItemCountUnbindPath=null;
		private string checkedLinesUnbindPath=null;
		private string lineSelectionUnbindPath=null;

        private IGridPageSizeResolver pageSizeResolver;
		
		#region Binding

		public string LineSelectionUnbindPath
		{
			get { return GetPropertyPath(lineSelectionUnbindPath, "LineSelection"); }
			set { lineSelectionUnbindPath = value; }
		}

		public string DataGridStatePath
		{
			set { dataGridStatePath = value; }
		}

//		public string VirtualItemCountBindPath
//		{
//			get { return GetPropertyPath(virtualItemCountBindPath, "VirtualItemCount");}
//			set { virtualItemCountBindPath = value; }
//		}

		public string VirtualItemCountUnbindPath
		{
			get { return GetPropertyPath(virtualItemCountUnbindPath, "VirtualItemCount"); }
			set { virtualItemCountUnbindPath = value; }
		}

		public string VirtualItemCountPath
		{
			set
			{
				virtualItemCountBindPath = value; 
				virtualItemCountUnbindPath = value;
			}
		}

		public string OperationNameBindPath
		{
			get
			{
				if (operationNameBindPath!=null)
				{
					return operationNameBindPath;
				}

				//For backward compatibility. Should be removed in future
				if (operationContextName!=null)
				{
					return String.Format("ec['{0}']", operationContextName);
				}
				if (operationParameterName!=null)
				{
					return String.Format("request['{0}']", operationParameterName);
				}
				//--

				return null;
			}
			set { operationNameBindPath = value; }
		}

        public string OperationNameUnbindPath
        {
            get
            {
                return operationNameUnbindPath;
            }
            set { operationNameUnbindPath = value; }
        }

        public string OperationNamePath
        {
            set
            {
                operationNameBindPath = value;
                operationNameUnbindPath = value;
            }
        }

		public string DataSourceBindPath
		{
			get { return GetPropertyPath(dataSourceBindPath, "DataSource"); }
			set { dataSourceBindPath = value; }
		}

		public string SortAscUnbindPath
		{
			get { return GetPropertyPath(sortAscUnbindPath, "SortAsc"); }
			set { sortAscUnbindPath = value; }
		}

		public string SortExpressionUnbindPath
		{
			get { return GetPropertyPath(sortExpressionUnbindPath, "SortExpression"); }
			set { sortExpressionUnbindPath = value; }
		}

		public string PageSizeUnbindPath
		{
			get { return GetPropertyPath(pageSizeUnbindPath, "PageSize"); }
			set { pageSizeUnbindPath = value; }
		}

		public string CurrentPageIndexUnbindPath
		{
			get { return GetPropertyPath(currentPageIndexUnbindPath, "CurrentPageIndex"); }
			set { currentPageIndexUnbindPath = value; }
		}


		public string CheckedLinesUnbindPath
		{
			get { return GetPropertyPath(checkedLinesUnbindPath, "CheckedLines"); }
			set { checkedLinesUnbindPath = value; }
		}

		private string GetPropertyPath(string particularPath, string dataGridStatePropertyName)
		{
			if (particularPath!=null)
			{
				return particularPath;
			}
			if (dataGridStatePath==null)
			{
				return null;
			}

			return String.Format(dataGridStatePath, dataGridStatePropertyName);
		}

		#endregion

		public DataGridConfiguration()
		{
		}

		public IDataGridController Controller
		{
			get { return controller;}
			set { controller = value;}
		}

	    public IGridPageSizeResolver PageSizeResolver
	    {
	        get { return pageSizeResolver; }
	        set { pageSizeResolver = value; }
	    }

	    public IList Columns
		{
			get { return columns; }
			set { columns = value; }
		}

		public bool ShowHeader
		{
			get { return showHeader; }
			set { showHeader = value; }
		}

		public bool ShowFooter
		{
			get { return showFooter; }
			set { showFooter = value; }
		}

	  

	    public int PageSize
		{
			get
			{
                if (pageSizeResolver == null)
                    return pageSize;

                int customPageSize = pageSizeResolver.PageSize;

                return (customPageSize > 0) ? customPageSize : pageSize;
			}
			set { pageSize = value; }
		}

		public bool AllowCustomPaging
		{
			get { return allowCustomPaging; }
			set { allowCustomPaging = value; }
		}

		public bool AllowPaging
		{
			get { return allowPaging; }
			set { allowPaging = value; }
		}

		public IDataGridPagerStyleConfiguration PagerStyle
		{
			get { return pagerStyle; }
			set { pagerStyle = value; }
		}

		public bool UsePrefixKeyForPager
		{
			get { return usePrefixKeyForPager; }
			set { usePrefixKeyForPager = value; }
		}

		public bool AllowSorting
		{
			get
			{
				if(columns == null)
				{
					return false;
				}
				foreach(IDataGridColumnConfiguration column in columns)
				{
					if(column.Sortable)
					{
						return true;
					}
				}
				return false;
			}
		}

		public IDataGridColumnConfiguration DefaultSortableColumn
		{
			get
			{
				if(columns == null)
				{
					return null;
				}
				foreach(IDataGridColumnConfiguration column in columns)
				{
					if(column.IsDefaultSort)
					{
						return column;
					}
				}
				return null;
			}
		}

		public string OperationParameterName
		{
			get { return operationParameterName; }
			set { operationParameterName = value; }
		}

		public string OperationContextName
		{
			get { return operationContextName; }
			set { operationContextName = value; }
		}

		public IDataGridLineSelector LineSelector
		{
			get { return lineSelector; }
			set { lineSelector = value; }
		}

	    //		[Obsolete]
//		public string DataCollectionPath
//		{
//			get
//			{
//				return dataSourceBindPath;				
////				if (!StringUtils.IsNullOrEmpty(dataSourceBindPath))
////				{
////					return dataSourceBindPath;
////				}
//
////				//TODO: obsolete. to be removed!
////				String defaultKey = String.Format("current{0}{1}", (StringUtils.IsNullOrEmpty(ExecutionContextPrefix) ? String.Empty : "."), ExecutionContextPrefix);
////				
////				return String.Format("{0}.data", defaultKey);
////				//--
//			}
//			set { dataSourceBindPath = value; }
//		}


		private static IDataGridLineSelector FAKE_LINE_SELECTOR_INSTANCE = new FakeLineSelector();
		public class FakeLineSelector : IDataGridLineSelector
		{
			public bool CheckIfSelected(object dataRow, IExecutionContext executionContext)
			{
				return false;
			}
		}
	}
}
